using System;
using System.Collections.Generic;
using System.Text;

namespace GeneticAlgorithm.CP1
{
    class Mutation
    {
        /// <summary>
        /// Given parent with genotype (x, y), randomly change x, or y, or both by
        /// a offset value in the range of [-1, 1]
        /// </summary>
        /// <param name="parent"></param>
        /// <returns></returns>
        public static Individual<float[], float[]> A(Individual<float[], float[]> parent)
        {
            float[] genotype = new float[] { parent.Genotype[0], parent.Genotype[1] };

            int mutateType = Global.random.Next(3);
            switch (mutateType)
            {
                case 0:
                    // mutate parent's gene x
                    genotype[0] += Global.NextGeneOffset();
                    break;
                case 1:
                    // mutate parent's gene y
                    genotype[1] += Global.NextGeneOffset();
                    break;
                case 2:
                    // mutate both parent's x and y
                    genotype[0] += Global.NextGeneOffset();
                    genotype[1] += Global.NextGeneOffset();
                    break;
            }

            genotype[0] = Clamp(genotype[0]);
            genotype[1] = Clamp(genotype[1]);

            Individual<float[], float[]> retval = new Individual(genotype, parent);
            //retval.Id.Add(parent.Id[0]);
            //retval.Id.Add(mutateType - 3);

            return retval;
        }

        /// <summary>
        /// Clamp the offsetted genotype to a range of [-5, 5]
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        private static float Clamp(float input)
        {
            if (input > 5f)
            {
                input = 5f;
            }
            else if (input < -5f)
            {
                input = -5f;
            }
            return input;
        }
    }
}
